package com.alks.function.service.impl.biocontrol.webwork;

import com.alibaba.excel.util.StringUtils;
import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.beanutils.ArrayUtils;
import com.alks.entity.data.entity.PsIeData;
import com.alks.entity.data.entity.PsLine;
import com.alks.entity.data.entity.PsLtData;
import com.alks.entity.data.page.PageParam;
import com.alks.entity.data.page.PageRecord;
import com.alks.function.mapper.biocontrol.webwork.PsIeDataMapper;
import com.alks.function.mapper.biocontrol.webwork.PsLineMapper;
import com.alks.function.mapper.biocontrol.webwork.PsLtDataMapper;
import com.alks.function.service.biocontrol.webwork.ILastConfigService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author MS
 * @since 2025/2/11上午9:13
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class LastConfigServiceImpl implements ILastConfigService {
    private final PsIeDataMapper psIeDataMapper;
    private final PsLtDataMapper psLtDataMapper;
    private final PsLineMapper psLineMapper;

    @Getter
    private enum Type {
        /**
         * 类型枚举
         */
        COMMON("通用","A"),
        BRAND("品牌","B"),
        STYLE("型体","S");

        private final String typeName;
        private final String typeCode;

        Type(String typeName,String typeCode) {
            this.typeName = typeName;
            this.typeCode = typeCode;
        }

        public static String getNameByCode(String code) {
            for (Type type : Type.values()) {
                if (type.getTypeCode().equals(code)) {
                    return type.getTypeName();
                }
            }
            return null;
        }

        public static String getCodeByName(String name) {
            for (Type type : Type.values()) {
                if (type.getTypeName().equals(name)) {
                    return type.getTypeCode();
                }
            }
            return null;
        }
    }

    @Override
    @AutoPageAop
    public PageRecord<PsIeData> ieQuery(PageParam request, String type, String brand, String style) {
        /*查询产能数据*/
        String companyId = UserIdThread.get().getCompanyId();
        type = Type.getCodeByName(type);
        LambdaQueryWrapper<PsIeData> wrapper = new LambdaQueryWrapper<PsIeData>()
                .eq(PsIeData::getCompanyId,companyId)
                .like(StringUtils.isNotBlank(type),PsIeData::getIeType,type)
                .like(StringUtils.isNotBlank(brand),PsIeData::getBrand,brand)
                .like(StringUtils.isNotBlank(style),PsIeData::getStyle,style)
                .last("ORDER BY IF(IE_TYPE = 'A',0,1),BRAND,IF(IE_TYPE = 'B',0,1)");
        List<PsIeData> dateList = psIeDataMapper.selectList(wrapper);
        dateList.forEach(data -> {
            data.setIeType(Type.getNameByCode(data.getIeType()));
        });
        return new PageRecord<>(dateList);
    }

    @Override
    public void ieUpdate(PsIeData request) {
        String companyId = UserIdThread.get().getCompanyId();
        /*更新数据*/
        LambdaUpdateWrapper<PsIeData> wrapper = new LambdaUpdateWrapper<PsIeData>()
                .set(PsIeData::getPpdQty,request.getPpdQty())
                .set(PsIeData::getIeQty,request.getIeQty())
                .eq(PsIeData::getId,request.getId())
                .eq(PsIeData::getCompanyId,companyId);
        psIeDataMapper.update(null,wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void ieStyleUp(Integer type, String brand) {
        String companyId = UserIdThread.get().getCompanyId();
        //A 通用 B 品牌 S 型体
        if (type.equals(1)){
            /*按品牌更新逻辑*/
            psIeDataMapper.ieBrandUp(companyId,brand);
        }else {
            String userName = UserIdThread.get().getUserName();
            /*获取需要写入的型体和品牌数据*/
            List<PsIeData> styleIeList = psIeDataMapper.ieBrandUpQuery(companyId,userName);
            for (PsIeData data : styleIeList) {
                psIeDataMapper.insert(data);
            }
            List<PsIeData> brandIeList = psIeDataMapper.ieStyleUpQuery(companyId,userName);
            for (PsIeData data : brandIeList) {
                psIeDataMapper.insert(data);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void ieDelete(List<Integer> idList) {
        /*删除品牌是否需要删除对应品牌下全部型体产能数据？*/
        psIeDataMapper.deleteBatchIds(idList);
    }

    @Override
    @AutoPageAop
    public PageRecord<PsLtData> timeQuery(PageParam request, String type, String brand, String style) {
        String companyId = UserIdThread.get().getCompanyId();
        /*获取数据*/
        type = Type.getCodeByName(type);
        LambdaQueryWrapper<PsLtData> wrapper = new LambdaQueryWrapper<PsLtData>()
                .eq(PsLtData::getCompanyId,companyId)
                .like(StringUtils.isNotBlank(type),PsLtData::getLtType,type)
                .like(StringUtils.isNotBlank(brand),PsLtData::getBrand,brand)
                .like(StringUtils.isNotBlank(style),PsLtData::getStyle,style)
                .last("ORDER BY IF(LT_TYPE = 'A',0,1),BRAND,IF(LT_TYPE = 'B',0,1)");
        List<PsLtData> dateList = psLtDataMapper.selectList(wrapper);
        dateList.forEach(data -> {
            data.setLtType(Type.getNameByCode(data.getLtType()));
        });
        return new PageRecord<>(dateList);
    }

    @Override
    public void timeUpdate(PsLtData request) {
        String companyId = UserIdThread.get().getCompanyId();
        /*更新数据*/
        LambdaUpdateWrapper<PsLtData> wrapper = new LambdaUpdateWrapper<PsLtData>()
                .set(PsLtData::getTrailLt,request.getTrailLt())
                .set(PsLtData::getMtlLt,request.getMtlLt())
                .set(PsLtData::getCut1Lt,request.getCut1Lt())
                .set(PsLtData::getCut2Lt,request.getCut2Lt())
                .set(PsLtData::getCut3Lt,request.getCut3Lt())
                .set(PsLtData::getStitchLt,request.getStitchLt())
                .set(PsLtData::getSoleLt,request.getSoleLt())
                .set(PsLtData::getLastLt,request.getLastLt())
                .eq(PsLtData::getId,request.getId())
                .eq(PsLtData::getCompanyId,companyId);
        psLtDataMapper.update(null,wrapper);
    }

    @Override
    public void timeStyleUp(Integer type, String brand) {
        String companyId = UserIdThread.get().getCompanyId();
        if (type.equals(1)){
            /*按品牌更新逻辑*/
            psLtDataMapper.ieBrandUp(companyId,brand);
        }else {
            String userName = UserIdThread.get().getUserName();
            /*获取需要写入的型体和品牌数据*/
            List<PsLtData> styleIeList = psLtDataMapper.ieBrandUpQuery(companyId,userName);
            for (PsLtData data : styleIeList) {
                psLtDataMapper.insert(data);
            }
            List<PsLtData> brandIeList = psLtDataMapper.ieStyleUpQuery(companyId,userName);
            for (PsLtData data : brandIeList) {
                psLtDataMapper.insert(data);
            }
        }
    }

    @Override
    public void timeDelete(List<Integer> idList) {
        String companyId = UserIdThread.get().getCompanyId();
        /*删除数据*/
        LambdaUpdateWrapper<PsLtData> wrapper = new LambdaUpdateWrapper<PsLtData>()
                .in(PsLtData::getId,idList)
                .eq(PsLtData::getCompanyId,companyId);
        psLtDataMapper.delete(wrapper);
    }

    @Override
    public List<String> lineBack(String search) {
        String companyId = UserIdThread.get().getCompanyId();
        /*获取数据*/
        LambdaQueryWrapper<PsLine> wrapper = new LambdaQueryWrapper<PsLine>()
                .select(PsLine::getLine)
                .eq(PsLine::getCompanyId,companyId)
                .like(PsLine::getLine,search)
                .groupBy(PsLine::getLine)
                .last(StringUtils.isNotBlank(search),"ORDER BY IF("+search+" = LINE,0,1)");
        List<PsLine> lines = psLineMapper.selectList(wrapper);
        if (ArrayUtils.isEmpyt(lines)){
            return List.of();
        }
        return lines.stream()
                .filter(Objects::nonNull)
                .map(PsLine::getLine)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }

    @Override
    public List<String> ieBrandBack(String search) {
        String companyId = UserIdThread.get().getCompanyId();
        /*获取数据*/
        LambdaQueryWrapper<PsIeData> wrapper = new LambdaQueryWrapper<PsIeData>()
                .select(PsIeData::getBrand)
                .eq(PsIeData::getCompanyId,companyId)
                .like(PsIeData::getBrand,search)
                .groupBy(PsIeData::getBrand)
                .last(StringUtils.isNotBlank(search),"ORDER BY IF("+search+" = LINE,0,1)");
        List<PsIeData> lines = psIeDataMapper.selectList(wrapper);
        if (ArrayUtils.isEmpyt(lines)){
            return List.of();
        }
        return lines.stream()
                .filter(Objects::nonNull)
                .map(PsIeData::getBrand)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }

    @Override
    public List<String> ltBrandBack(String search) {
        String companyId = UserIdThread.get().getCompanyId();
        /*获取数据*/
        LambdaQueryWrapper<PsLtData> wrapper = new LambdaQueryWrapper<PsLtData>()
                .select(PsLtData::getBrand)
                .eq(PsLtData::getCompanyId,companyId)
                .like(PsLtData::getBrand,search)
                .groupBy(PsLtData::getBrand)
                .last(StringUtils.isNotBlank(search),"ORDER BY IF("+search+" = LINE,0,1)");
        List<PsLtData> lines = psLtDataMapper.selectList(wrapper);
        if (ArrayUtils.isEmpyt(lines)){
            return List.of();
        }
        return lines.stream()
                .filter(Objects::nonNull)
                .map(PsLtData::getBrand)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }
}
